package com.wiegand.content {
	import com.wiegand.tracking.GoogleAnalyticsTracker;
	import com.asual.swfaddress.SWFAddress;
	import com.asual.swfaddress.SWFAddressEvent;
	import com.wiegand.Configuration;
	import com.wiegand.event.ContentStatusEvent;

	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * @author gandy
	 */
	 
	public class ContentManager extends Sprite {
		
		private var _currentcontent : AbstractContent = null;
		private var _newcontent : AbstractContent = null;
		private var _currentname : String = null;
		private var _newname : String = null;
		private var _transition : Boolean = false;
		
		public function ContentManager() {
			SWFAddress.setTitle("wie? gand! interactive");
			
			SWFAddress.addEventListener(SWFAddressEvent.INIT, swfAdressInit);
			SWFAddress.addEventListener(SWFAddressEvent.CHANGE, swfAdressChange);
		}
		
		private function swfAdressInit(e:SWFAddressEvent):void {
			SWFAddress.removeEventListener(SWFAddressEvent.INIT, swfAdressInit);
			
			var startContent : String = null;
			 
			if( 
				String(Configuration.getInstance().contents.@startContent) != null &&
				String(Configuration.getInstance().contents.@startContent) != "undefined"			
			 ){
				startContent = String(Configuration.getInstance().contents.@startContent);
			} 
			
			if( 
				String(SWFAddress.getPathNames()[0]) != null &&
				String(SWFAddress.getPathNames()[0]) != "undefined"
			){
				startContent = String(SWFAddress.getPathNames()[0]);
			}
			
			if(startContent!=null) SWFAddress.setValue(startContent);
		}
		
		private function swfAdressChange(e:SWFAddressEvent):void {
			if ( valid(e.pathNames) ) changeContent(e.pathNames[0]);
			else SWFAddress.back();
		}		

		private function valid(path:Array):Boolean{
			var v : Boolean = false;
			for (var i : int = 0; i < Configuration.getInstance().contents.children().length(); i++) {
				if(path[0] ==  Configuration.getInstance().contents.child(i).@name) v=true;
			}
			return v;
		}

		private function changeContent(contentname:String):void{
			_newname = contentname;
			if(!_transition){
				_transition = true;
				if(_currentcontent==null && _currentname!=_newname) openNewContent();
				else closeCurrentContent();
			}
		}
		
		private function closeCurrentContent():void {
			_currentcontent.addEventListener(ContentStatusEvent.STATE_CLOSED, removeOldContent);
			_currentcontent.close();
		}
		
		private function removeOldContent(e:Event) : void {
			Configuration.getInstance().dict.removeByName(_currentname);
			
			_currentcontent.removeEventListener(ContentStatusEvent.STATE_CLOSED, removeOldContent);
			removeChild(_currentcontent);
			_currentcontent.destroy();
			_currentcontent = null;
			_currentname = null;
			
			if(_newname != null) openNewContent();
		}

		private function openNewContent():void{
			var contentClass : Class = AbstractContentClasses.getClass(getClassName(_newname));
			_newcontent = new contentClass( getContentNode(_newname) );
			addChild(_newcontent);
			
			Configuration.getInstance().dict.add(_newcontent, _newname);
			
			_newcontent.addEventListener(ContentStatusEvent.STATE_OPEN, completeOpen);
			_newcontent.open();
		}
		
		private function completeOpen(e:Event):void{
			_newcontent.removeEventListener(ContentStatusEvent.STATE_OPEN, completeOpen);
			_transition = false;
			_currentcontent = _newcontent;
			_currentname = _newname;
			_newcontent = null;
			_newname = null;
			GoogleAnalyticsTracker.getInstance().track("content:"+_currentname);
		}
		
		private function getClassName(name:String):String{
			var classname : String;
			for (var i : int = 0; i < Configuration.getInstance().contents.children().length(); i++) {
				var child : XMLList = Configuration.getInstance().contents.child(i);
				if( child.@name == name ) classname = child.@classname;
			}
			return classname;
		}
		
		private function getContentNode(name:String):XMLList{
			var node : XMLList;
			for (var i : int = 0; i < Configuration.getInstance().contents.children().length(); i++) {
				var child : XMLList = Configuration.getInstance().contents.child(i);
				if( child.@name == name ) node = child;
			}
			return node;
		}
		
	}
}
